struct vertex
{
	float4 position : POSITION;
    float4 normal   : NORMAL;
	float4 color0   : COLOR0;
	float2 bounds0  : TEXCOORD0;
	float2 bounds1	: TEXCOORD1;
	float2 bounds2  : TEXCOORD2;
	float2 bounds3	: TEXCOORD3;
};

struct fragment
{
	float4 position : POSITION;
	float4 color0   : COLOR0;
};

uniform float4x4 modelViewProj;

uniform float4 light;

//matrices for right lower leg movement
uniform float4x4 toOrigin0;
uniform float4x4 rot0;
uniform float4x4 fromOrigin0;

//matrices for left lower leg movement
uniform float4x4 toOrigin1;
uniform float4x4 rot1;
uniform float4x4 fromOrigin1;

//matrices for right leg movement

uniform float4x4 toOrigin2;
uniform float4x4 rot2;
uniform float4x4 fromOrigin2;

//matrices for left leg movement

uniform float4x4 toOrigin3;
uniform float4x4 rot3;
uniform float4x4 fromOrigin3;

fragment main(vertex IN)
{
	fragment OUT;
	
	
	float4x4 lastMatrix = modelViewProj;
	float4x4 matrix;
	
	float4 position = {0.0, 0.0, 0.0, 1.0};
	float4 normal = {0.0, 0.0, 0.0, 1.0};
	
	matrix = mul(lastMatrix,fromOrigin2);
	matrix = mul(matrix,rot2);
	matrix = mul(matrix,toOrigin2);
	
	lastMatrix = matrix * IN.bounds2[0] + lastMatrix * IN.bounds2[1];
	
	//Left
	matrix = mul(lastMatrix,fromOrigin3);
	matrix = mul(matrix,rot3);
	matrix = mul(matrix,toOrigin3);
	
	lastMatrix = matrix * IN.bounds3[0] + lastMatrix * IN.bounds3[1];

	//Lower Right
	matrix = mul(lastMatrix,fromOrigin0);
	matrix = mul(matrix,rot0);
	matrix = mul(matrix,toOrigin0);

	lastMatrix = matrix * IN.bounds0[0] + lastMatrix * IN.bounds0[1];

	//Lower Left
	matrix = mul(lastMatrix,fromOrigin1);
	matrix = mul(matrix,rot1);
	matrix = mul(matrix,toOrigin1);
	
	lastMatrix = matrix * IN.bounds1[0] + lastMatrix * IN.bounds1[1];
	
	OUT.position = mul(lastMatrix,IN.position);	
	OUT.color0 = IN.color0*dot(light, IN.normal);

	return OUT;
}

technique twist {
  pass {
    VertexProgram = compile arbvp1 main();
  }
}